<template>
  <div class="page-login">
    <load-more :show-loading="false" tip="自动登陆" background-color="#fbf9fe"></load-more>
  </div>
</template>

<script>
  import { LoadMore, base64, querystring } from 'vux'
  export default {
    name: 'LoginPage',
    components: {
      LoadMore
    },
    /**
     * 创建实例时执行
     */
    created: function () {
      this.$vux.loading.show({
        text: '正在登陆'
      })
      if (this.$wxTool.isClient()) {
        this.wxLogin()
      } else {
        // TODO 跳转到错误页
        this.$router.push({path: '/error'})
      }
    },
    /**
     * 方法列表
     */
    methods: {
      /**
       * 微信登陆
       */
      wxLogin: function () {
        let token = this.$wxTool.getToken(this.$appConfig.wx_official)
        if (!token) {
          // TODO  临时使用
          let url = window.location.href
          let hash = window.location.hash
          let tokenStart = hash.indexOf('?wx_token=')
          if (tokenStart !== -1) {
            this.$vux.loading.hide()
            let param = hash.substr(tokenStart + 1)
            param = querystring.parse(param)
            if (!param.wx_token) {
              this.$vux.alert.show({
                title: '错误',
                content: '授权失败，请联系微信/QQ:229417598反馈~'
              })
              return false
            }
            this.$cookie.set('wx_token_' + this.$appConfig.wx_official, param.wx_token)
            // 跳转到首页
            // this.$router.push({path: '/'})
            this.$vux.alert.show({
              title: '提示',
              content: '<div>登陆成功</div><div style="font-size: 14px;">如未跳转请刷新页面</div>',
              onHide () {
                window.location.reload()
              }
            })
            return false
          }
          window.location.href = 'http://wx.0817ch.com/koi_yehoo/user/login.html?callback=' + base64.encode(url)
          return false
        }
        // TODO 临时跳转到首页
        this.$vux.loading.hide()
        this.$router.push({path: '/'})
      }
    }
  }
</script>

<style scoped>
</style>